package com.shr.lease.web.app.service.impl;

import com.shr.lease.common.context.LoginUserContext;
import com.shr.lease.model.entity.*;
import com.shr.lease.model.enums.ItemType;
import com.shr.lease.web.app.mapper.*;
import com.shr.lease.web.app.service.LeaseAgreementService;
import com.shr.lease.web.app.vo.agreement.AgreementDetailVo;
import com.shr.lease.web.app.vo.agreement.AgreementItemVo;
import com.shr.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Resource
    private LeaseAgreementMapper leaseAgreementMapper;
    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Resource
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Resource
    private PaymentTypeMapper paymentTypeMapper;
    @Resource
    private LeaseTermMapper leaseTermMapper;

    @Override
    public List<AgreementItemVo> listAgreementItemVo() {
        return leaseAgreementMapper.listAgreementItemVo(LoginUserContext.getLoginUser().getUserId());
    }

    @Override
    public AgreementDetailVo getDetailById(Long id) {
        //基本信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        //公寓名称
        Long apartmentId = leaseAgreement.getApartmentId();
        String apartmentName = apartmentInfoMapper.selectNameById(apartmentId);
        //公寓图片
        List<GraphVo> apartmentGraphVos = graphInfoMapper.listGraphVo(apartmentId, ItemType.APARTMENT);
        //房间号
        Long roomId = leaseAgreement.getRoomId();
        String roomNumber = roomInfoMapper.selectRoomNumberById(roomId);
        //房间图片列表
        List<GraphVo> roomGraphVos = graphInfoMapper.listGraphVo(roomId, ItemType.ROOM);
        //支付方式名称
        Long paymentTypeId = leaseAgreement.getPaymentTypeId();
        String paymentTypeName = paymentTypeMapper.selectNameById(paymentTypeId);
        //租期月数
        //Date类转换为LocalDate
        LocalDate startDate = leaseAgreement.getLeaseStartDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endDate = leaseAgreement.getLeaseEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        int leaseTremMonthCount =(int) ChronoUnit.MONTHS.between(startDate,endDate);
        //租期单位
        Long leaseTermId = leaseAgreement.getLeaseTermId();
        String leaseTermUnit = leaseTermMapper.selectUnitById(leaseTermId);

        //组装属性
        AgreementDetailVo agreementDetailVo = AgreementDetailVo.builder()
                .apartmentName(apartmentName)
                .apartmentGraphVoList(apartmentGraphVos)
                .roomNumber(roomNumber)
                .roomGraphVoList(roomGraphVos)
                .paymentTypeName(paymentTypeName)
                .leaseTermMonthCount(leaseTremMonthCount)
                .leaseTermUnit(leaseTermUnit)
                .build();
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        return agreementDetailVo;
    }
}




